package com.lb.bs.client.processor.handle.statics.third;

import com.lb.bs.client.annotation.ProcessorMark;
import com.lb.bs.client.annotation.SingletonMark;
import com.lb.bs.client.beans.GroupInfo;
import com.lb.bs.client.beans.LBGrayIPBean;
import com.lb.bs.client.beans.LBGrayItemBean;
import com.lb.bs.client.beans.LBItemBean;
import com.lb.bs.client.repository.ScanStaticModel;
import com.lb.bs.client.repository.StoreCenter;
import com.lb.bs.client.factory.SingletonFactory;
import com.lb.bs.client.processor.ProcessorService;
import com.lb.bs.client.processor.enums.ProcessorType;
import com.lb.bs.client.util.CollectionUtils;
import com.lb.bs.client.util.StringUtils;
import com.lb.bs.client.zookeeper.FetchMgr;
import com.lb.bs.client.zookeeper.ZKMgr;

import java.util.Map;
import java.util.Set;

/**
 * program: LB-BS-Client
 * author: bsworld.xie
 * create: 2019-12-11 16:37
 * description:
 */
@SingletonMark
@ProcessorMark(type = ProcessorType.THIRD_WATCH_REMOTE_RELOAD)
public class StaticWatchRemoteProcessor implements ProcessorService<ScanStaticModel> {


    @Override
    public void process(ScanStaticModel scanStaticModel) {

        Map<String, GroupInfo> groupInfoMap = StoreCenter.getInstance().getGroupInfoMap();
        Set<String> groupNameSet = groupInfoMap.keySet();

        ZKMgr zkMgr = ZKMgr.getInstance();
        if (CollectionUtils.isNotEmpty(groupNameSet)) {
            //灰度路径包含在group路径下，可以实现监听
            for (String groupName : groupNameSet) {
                zkMgr.watch(groupName);
            }
        }
        Map<String, LBItemBean> itemKeyMap = StoreCenter.getInstance().getItemKeyMap();


        FetchMgr fetchMgr = SingletonFactory.getInstance(FetchMgr.class);

        for (Map.Entry<String, LBItemBean> beanEntry : itemKeyMap.entrySet()) {
            String itemKey = beanEntry.getKey();
            LBItemBean itemBean = beanEntry.getValue();
            if (StringUtils.isEmpty(itemKey) || itemBean == null) {
                return;
            }
//            //不同于动态同步,静态同步对enable的不进行更新
//            if (Boolean.TRUE.equals(itemBean.getEnable())) {
//                return;
//            }
            fetchMgr.reloadOneItem(itemKey);
        }
        Map<String, LBGrayIPBean> grayIPKeyMap = StoreCenter.getInstance().getGrayIPKeyMap();
        for (Map.Entry<String, LBGrayIPBean> grayIPBeanEntry : grayIPKeyMap.entrySet()) {
            LBGrayIPBean ipBean = grayIPBeanEntry.getValue();
            if (ipBean == null) {
                continue;
            }
            String grayIPKey = grayIPBeanEntry.getKey();
            String groupName = ipBean.getGroupName();
            GroupInfo groupInfo = groupInfoMap.get(groupName);
            if (groupInfo == null) {
                continue;
            }
            boolean enableGray = groupInfo.isEnableGray();
            if (!enableGray) {
                continue;
            }
            fetchMgr.reloadOneItem(grayIPKey);
        }

        Map<String, LBGrayItemBean> grayItemKeyMap = StoreCenter.getInstance().getGrayItemKeyMap();
        for (Map.Entry<String, LBGrayItemBean> grayItemBeanEntry : grayItemKeyMap.entrySet()) {
            String grayItemKey = grayItemBeanEntry.getKey();
            LBGrayItemBean grayItemBean = grayItemBeanEntry.getValue();
            if (grayItemBean == null ) {
                continue;
            }

            String groupName = grayItemBean.getGroupName();

            GroupInfo groupInfo = groupInfoMap.get(groupName);
            if (groupInfo == null) {
                continue;
            }
            boolean enableGray = groupInfo.isEnableGray();
            if (!enableGray) {
                continue;
            }
            fetchMgr.reloadOneItem(grayItemKey);
        }
    }


}
